﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Core.ServiceImpl.Repositories.Interface;
using EzDesk.Core.Model;
using System.Data.Linq.SqlClient;
using EzDesk.Utility;

namespace EzDesk.Core.ServiceImpl.Repositories
{
    public partial class AppFunctionRepository : IAppFunctionRepository
    {
        EzDeskDataContext _db;
        IAppFunctionTypeRepository _appFunctionTypeRepository;

        public AppFunctionRepository(EzDeskDataContext db, IAppFunctionTypeRepository appFunctionTypeRepository)
        {
            this._db = db;
            this._appFunctionTypeRepository = appFunctionTypeRepository;
        }

        public void SaveChanges()
        {
            _db.SubmitChanges();
        }


        public IList<AppFunction> GetAppFunctionsbyUserAgent(EzDeskUserAgentType userAgent)
        {
            var query = _db.ez_AppFunctions.Where(p => SqlMethods.Like(p.UserAgent, "%" + userAgent.ToString() + "%"));

            return query
                .Select(p => new AppFunction()
                                 {
                                     Id = p.AppFunctionId,
                                     AppName = p.AppName,
                                     IconName = p.IconName,
                                     FuncUrl = p.FuncUrl,
                                     Title = p.Title,
                                     Description = p.Description,
                                     Privilege = p.Privilege,
                                     PermissionNeed = p.PermissionNeed,

                                     Type = _appFunctionTypeRepository.GetFunctionTypeById(p.FunctionTypeId),
                                     Category = _appFunctionTypeRepository.GetCategotyTypeById(p.FunctionCategoryId),
                                 }
                )
                .ToList();
        }

        public string GetAppFunctionShortcutIds(Guid userId)
        {
            IList<Guid> result = new List<Guid>();

            var item = _db.ez_AppFunctionShortcuts.Where(p => p.UserId == userId).FirstOrDefault();
            if (item != null)
            {
                var idStrs = item.ShortcutSequence.Split(',');
                foreach (var idStr in idStrs)
                {
                    Guid id = Guid.Empty;
                    if (Guid.TryParse(idStr, out id))
                    {
                        result.Add(id);
                    }
                }
            }

            return result.Join(",");
        }

        public void UpdateAppFunctionShortcut(Guid userId, string shortcutIdSequnces)
        {
            var item = _db.ez_AppFunctionShortcuts.Where(p => p.UserId == userId).FirstOrDefault();

            if (item != null)
            {
                item.ShortcutSequence = shortcutIdSequnces;
            }
            else
            {
                var entity = new ez_AppFunctionShortcut()
                                 {
                                     UserId = userId,
                                     ShortcutSequence = shortcutIdSequnces,
                                 };
                _db.ez_AppFunctionShortcuts.InsertOnSubmit(entity);
            }
            SaveChanges();
        }
    }
}
